package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhs.storage.bean.BaseDepartment;
import com.hhs.storage.bean.BaseDepartmentUser;
import com.hhs.storage.bean.CarLog;
import com.hhs.storage.dao.BaseDepartmentUserDao;
import com.hhs.storage.service.BaseDepartmentUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hhs
 * @since 2023-01-12
 */
@Service
@Transactional
public class BaseDepartmentUserServiceImpl extends ServiceImpl<BaseDepartmentUserDao, BaseDepartmentUser> implements BaseDepartmentUserService {
    /**
     * 判断用户是否部门负责人,有返回部门Id则是负责人
     *
     * @param userId
     * @return
     */
    public String isDepartHead(String userId) {
        QueryWrapper<BaseDepartmentUser> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("type", 1);
        Page<BaseDepartmentUser> result = this.page(new Page<BaseDepartmentUser>(
                1,
                1), qw);
        List<BaseDepartmentUser> list=result.getRecords();
        if (list.size()==0){
            return null;
        }
        return list.get(0).getDepartId();
    }

    /**
     * 根据用户id,获取用户部门信息
     * @param userId
     * @return
     */
    public BaseDepartmentUser getByUserId(String userId) {
        QueryWrapper<BaseDepartmentUser> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        Page<BaseDepartmentUser> result = this.page(new Page<BaseDepartmentUser>(
                1,
                1), qw);
        List<BaseDepartmentUser> list=result.getRecords();
        if (list.size()==0){
            return null;
        }
        return list.get(0);
    }
}
